﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace FlexControls
{
    public partial class fcButton : UserControl
    {
        public fcButton()
        {
            InitializeComponent();
        }
        private string strWarning = "";
        public string fcWarningStringDel
        {
            get
            {
                return strWarning;
            }
            set
            {
                strWarning=value;
            }
        }
    
        public int intState = 0;
        public event EventHandler Click;
        protected virtual void OnClick()
        {
            if (this.Click != null)
            {
                this.Click(this, EventArgs.Empty);
                
            }
        }

        private void butThoat_Click(object sender, EventArgs e)
        {
            intState = flexControls.STATE_THOAT;
            OnClick();
        }

        private void fcButton_SizeChanged(object sender, EventArgs e)
        {
            this.Height = 44;
        }

        private void butThem_Click(object sender, EventArgs e)
        {
            if (intState == flexControls.STATE_UPDATE)
            {
                intState = flexControls.STATE_CLEAR;
                butXoa.Enabled = false;
                butCapNhat.Enabled = false;
                butThem.Text = "Lưu";
            }
            else
                intState = flexControls.STATE_ADD;
            OnClick();
        }

        private void butXoa_Click(object sender, EventArgs e)
        {
            string strDelete="Bạn có chắc là muốn xóa?\nViệc xóa này sẽ ảnh hưởng đến các dữ liệu khác";
            if(strWarning!="")
            {
                strDelete=strWarning;
            }
            if (MessageBox.Show(strDelete, "Xóa dữ liệu",MessageBoxButtons.YesNo )==DialogResult.Yes)
            {
                intState = flexControls.STATE_DELETE;
                butXoa.Enabled = false;
                butCapNhat.Enabled = false;
                butThem.Text = "Lưu";
                OnClick();
            }
        }

        private void butCapNhat_Click(object sender, EventArgs e)
        {
            intState = flexControls.STATE_UPDATE;
            butCapNhat.Enabled = false;
            butXoa.Enabled = false;
            butThem.Text = "Lưu";
            OnClick();
            
        }

        public void ChangeState(int butState)
        {
            intState = butState;
            if (intState == flexControls.STATE_ADD)
            {
                butXoa.Enabled = false;
                butCapNhat.Enabled = false;
                butThem.Text = "Lưu";
            }
            else 
            {
                butXoa.Enabled = true;
                butCapNhat.Enabled = true;
                butThem.Text = "Thêm mới";
            }
        }

       
    
    }
}
